//
//  TabBarView.m
//  TabBar
//
//  Created by 温人米 on 2019/1/9.
//  Copyright © 2019年 温人米. All rights reserved.
//

#import "TabBarView.h"
#import "RMTabBarButton.h"

@interface TabBarView ()

@property (nonatomic, strong)RMTabBarButton *buttonOne;
@property (nonatomic, strong)RMTabBarButton *buttonTwo;
@property (nonatomic, strong)RMTabBarButton *buttonCenter;
@property (nonatomic, strong)RMTabBarButton *buttonThress;
@property (nonatomic, strong)RMTabBarButton *buttonFour;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, strong)NSArray *array;

@end

@implementation TabBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor orangeColor];
        
        
    }
    return self;
}

-(void)layoutSubviews{
    [self layout];
    self.selectIndex = 0;
}

-(void)layout{
    
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    self.buttonOne.frame = CGRectMake(0, 0, buttonW, buttonH);
    self.buttonTwo.frame = CGRectMake(buttonW, 0, buttonW, buttonH);
    
    CGFloat centerWH = buttonH + 15;
    CGFloat centerX = (self.frame.size.width - centerWH) / 2;
    self.buttonCenter.frame = CGRectMake(centerX, - centerWH / 2 + 5, centerWH, centerWH);
    
    _buttonCenter.clipsToBounds = YES;
    _buttonCenter.layer.cornerRadius = centerWH / 2;
    
    self.buttonThress.frame = CGRectMake(buttonW * 3, 0, buttonW, buttonH);
    self.buttonFour.frame = CGRectMake(buttonW * 4, 0, buttonW, buttonH);
    self.array = self.subviews;
    
}

#pragma mark - 按钮的点击事件
-(void)buttonClick:(RMTabBarButton *)button{
    self.selectIndex = button.tag;
    if ([self.viewDelegate respondsToSelector:@selector(TabBarView:didSelectItemAtIndex:)]) {
        [self.viewDelegate TabBarView:self didSelectItemAtIndex:button.tag];
    }
}

#pragma mark - 中间按钮的点击事件
-(void)buttonCenterClick:(RMTabBarButton *)button{
    
    if ([self.viewDelegate respondsToSelector:@selector(TabBarViewDidClickCenterItem:)]) {
        [self.viewDelegate TabBarViewDidClickCenterItem:self];
    }
}

-(void)setSelectIndex:(NSInteger)selectIndex{
    
    RMTabBarButton *button = self.array[_selectIndex];
    //button.enabled = YES;
    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    RMTabBarButton *button2 = self.array[selectIndex];
    //button2.enabled = NO;
    [button2 setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    _selectIndex = selectIndex;
}

-(void)button:(RMTabBarButton *)button buttonTitle:(NSString *)string{
    button.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [button setTitle:string forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
}

-(RMTabBarButton *)buttonOne{
    if (!_buttonOne) {
        _buttonOne = [RMTabBarButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:_buttonOne];
        [self button:_buttonOne buttonTitle:@"首页"];
        _buttonOne.tag = 0;
        //_buttonOne.backgroundColor = [UIColor whiteColor];
    }
    return _buttonOne;
}

-(RMTabBarButton *)buttonTwo{
    if (!_buttonTwo) {
        _buttonTwo = [RMTabBarButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:_buttonTwo];
        [self button:_buttonTwo buttonTitle:@"活动"];
        _buttonTwo.tag = 1;
    }
    return _buttonTwo;
}

-(RMTabBarButton *)buttonCenter{
    if (!_buttonCenter) {
        _buttonCenter = [[RMTabBarButton alloc] init];
        [self addSubview:_buttonCenter];
        [_buttonCenter setTitle:@"+" forState:(UIControlStateNormal)];
        [_buttonCenter setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
        _buttonCenter.backgroundColor = [UIColor whiteColor];
        _buttonCenter.tag = 2;
        [_buttonCenter addTarget:self action:@selector(buttonCenterClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _buttonCenter;
}

-(RMTabBarButton *)buttonThress{
    if (!_buttonThress) {
        _buttonThress = [RMTabBarButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:_buttonThress];
        [self button:_buttonThress buttonTitle:@"发现"];
        _buttonThress.tag = 3;
//        _buttonThress.backgroundColor = [UIColor whiteColor];
    }
    return _buttonThress;
}

-(RMTabBarButton *)buttonFour{
    if (!_buttonFour) {
        _buttonFour = [RMTabBarButton buttonWithType:(UIButtonTypeCustom)];
        [self addSubview:_buttonFour];
        [self button:_buttonFour buttonTitle:@"我的"];
        _buttonFour.tag = 4;
        //_buttonFour.backgroundColor = [UIColor whiteColor];
    }
    return _buttonFour;
}




@end
